package com.luxf.base.domainevent.context;

import java.util.Optional;

public class LocalUserHolder {
    private static final ThreadLocal<LocalUser> USER_THREAD_LOCAL = new ThreadLocal<>();

    public static Optional<LocalUser> getLocalUser() {
        return Optional.ofNullable(USER_THREAD_LOCAL.get());
    }

    public static void setLocalUser(LocalUser obj) {
        if (obj == null) {
            clear();
            return;
        }
        USER_THREAD_LOCAL.set(obj);
    }

    public static void clear() {
        USER_THREAD_LOCAL.remove();
    }
}
